package com.app.pojo;

import com.app.utils.Judges;
import com.app.config.Settings;

import java.io.File;

public class MyFile{

    private String directory,name,md5;
    private Long lastModified,size;

    private boolean isFile;
    private String infoStr;

//    /**
//     *
//     * @param directory 目录
//     * @param infoStr 值，存储了文件信息 格式为：
//     */
//    public MyFile(String directory, String name,String infoStr) {
//        directory=directory.replaceAll("/+|\\\\+","/");
//        this.infoStr=infoStr;
//        try {
//            if (infoStr.startsWith(":")){
//                isFile=false;
//                int subid=infoStr.indexOf("|");
//                name=infoStr.substring(1,subid);
//                lastModified=Long.parseLong(infoStr.substring(subid+1));
//            }else {
//                isFile=true;
//                String[] sp=infoStr.split("\\|");
//                this.directory=directory;
//                name=sp[0];
//                md5=sp[1];
//                lastModified=Long.parseLong(sp[2]);
//                size=Long.parseLong(sp[3]);
//            }
//        }catch (Exception ignored){}
//    }

    public MyFile(String pathname){
        pathname=pathname.replaceAll("/+|\\\\+","/");
        if (pathname.endsWith("/")) pathname=pathname.substring(0,pathname.length()-1);

        int pos = pathname.lastIndexOf("/");
        if (pos>0){
            name=pathname.substring(pos+1);
            directory=pathname.substring(0,pos);
        } else{
            directory=pathname;
        }
        infoStr=name;
    }
    public MyFile(MyFile myFile,String child){
        child=child.replaceAll("/+|\\\\+","/");
        int pos = child.lastIndexOf("/");
        if (pos>0){
            directory=myFile.getAbsolutePath()+"/"+child.substring(0,pos);
            name=child.substring(pos+1);
            infoStr=name;
        }else if (pos<0){
            directory=myFile.getAbsolutePath();
            name=child;
            infoStr=name;
        }else {
            this.isFile=myFile.isFile;
            this.name=myFile.name;
            this.infoStr=myFile.infoStr;
            this.directory=myFile.directory;
            this.lastModified= myFile.lastModified;
            this.size= myFile.size;
        }
        directory=directory.replaceAll("/+|\\\\+","/");
    }
    public MyFile(String directory, String name, String md5, Long lastModified,Long size) {
        directory=directory.replaceAll("/+|\\\\+","/");
        this.directory = directory;
        this.name = name;
        this.md5 = md5;
        this.lastModified = lastModified;
        this.size=size;
        isFile=true;
        infoStr=md5+"|"+lastModified+"|"+size;
    }

    public boolean isFile() {
        return isFile;
    }

    public Long getSize() {
        return size;
    }

    public String getDirectory() {
        return directory;
    }

    public String getAbsolutePath() {
        if (name==null) return directory;
        return directory+"/"+name;
    }

    public String getName() {
        return name;
    }

    public String getMd5() {
        return md5;
    }

    public Long getLastModified() {
        return lastModified;
    }

    public boolean isAvailable() {
        return Judges.judgePath(directory)&&(name==null||Judges.judgeFilename(name))&&(md5==null||Judges.judgeMD5(md5));
    }

    public String getType(){
            return (isFile)? name.substring(name.lastIndexOf(".")):"dir";
    }
    public File getRealFile(String rootsDir){
        return new File(rootsDir+"/"+md5+getType());
    }
    public File getRealFile(){
        return new File(Settings.rootsDir+"/"+md5+getType());
    }
    public String getInfoStr(){
        return infoStr;
    }
    @Override
    public String toString() {
        return getAbsolutePath();
    }
}
